﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Extension;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;

namespace Bzzb.Controllers
{
    public class BaoZhangZhuangBeiIOTController : BaseController
    {
        private readonly IWebHostEnvironment _env;
        private readonly IConfiguration _configuration;
        private readonly ILogger<BaoZhangZhuangBeiIOTController> _logger;
        public BaoZhangZhuangBeiIOTController(ILogger<BaoZhangZhuangBeiIOTController> logger, IFreeSql fsql, IConfiguration configuration, IWebHostEnvironment env) : base(fsql)
        {
            this._env = env;
            this._logger = logger;
            this._configuration = configuration;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long bzzbtid, [FromForm] long bzzbid, [FromForm] long jtid, [FromForm] int doPage = 1, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            long total = 0;
            List<BaoZhangZhuangBeiIOT> baozhangzhuangbeiiots;
            if (doPage == 0)
            {
                baozhangzhuangbeiiots = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == bzzbid && bzzbiot.Jtid == jtid && bzzbiot.Gjid == 0).OrderBy("id desc").ToListAsync();
            }
            else {
                baozhangzhuangbeiiots = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == bzzbid && bzzbiot.Jtid == jtid && bzzbiot.Gjid == 0).OrderBy("id desc").Count(out total).Page(pageNumber, pageSize).ToListAsync();
                
            }
            JObject data = new();
            data.Add("total", total);
            data.Add("list", baozhangzhuangbeiiots == null ? null : JArray.FromObject(baozhangzhuangbeiiots));
            return RenderJson(data: data);
        }
    }
}
